﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
 * 情景要求： 假如有西施、王昭君、貂蝉、杨玉环四大美女同你相亲，你可以选择其中一位作为你的爱人；
 * 当然，如果你对前面的选择不满意，还可以重新选择，但希望你不要太花心；
 * 这个游戏提供后悔功能，用“备忘录模式”设计比较合适
 */
namespace Memont
{
    //妻子信息备忘录
    public class WifeMemento
    {
        string Name { get; set; }
        string Nation { get; set; }
        int Age { get; set; }

        internal WifeMemento SaveWifeInfo(WifeBase wife)
        {
            this.Name = wife.Name;
            this.Nation = wife.Nation;
            this.Age = wife.Age;
            return this;
        }

        internal void ResetWfieInfo(WifeBase wife)
        {
            wife.Name = this.Name;
            wife.Nation = this.Nation;
            wife.Age = this.Age;
        }

    }

    //妻子基类
    public class WifeBase
    {
        protected string name = string.Empty;
        public string Name { get => name; set => this.name = value; }
        public string Nation { get; set; }
        public int Age { get; set; }

    }

    #region 各种妻子

    public class XishiWife : WifeBase
    {
        public XishiWife()
        {
            this.name = "西施";
        }
    }

    public class WZJWife : WifeBase
    {
        public WZJWife()
        {
            this.name = "王昭君";
        }
    }

    public class DiaoChanWife : WifeBase
    {
        public DiaoChanWife()
        {
            base.name = "貂蝉";
        }
    }

    public class YYHWife : WifeBase
    {
        public YYHWife()
        {
            this.name = "杨玉环";
        }
    }
    #endregion

    //备忘录管理者
    public class WifeStack
    {
        const int capactiy = 4;

       // Stack<WifeMemento> WifeStackIoc = new Stack<WifeMemento>();
        Queue<WifeMemento> WifeQueue = new Queue<WifeMemento>(capactiy);
        public bool Enqueue(WifeMemento wife)
        {
            if (this.WifeQueue.Count == capactiy)
            {
                return false;
            }
            WifeQueue.Enqueue(wife);
            return true;
        }


        public WifeMemento Dequeue()
        {
            if (WifeQueue.Count == 0)
            {
                return null;
            }
            return WifeQueue.Dequeue();

        }
    }

    //发起者
    public class Oneself
    {
        public WifeBase MyWife { get; private set; }

        WifeStack wifeStack = new WifeStack();

        //选择妻子
        public bool SelectWife(WifeType type)
        {
            if (MyWife == null)
            {
                this.MyWife = WifeFactory.CreateWife(type);
                return true;
            }
            else
            {
                //保存变化前的数据
                bool saveFlag = wifeStack.Enqueue(new WifeMemento().SaveWifeInfo(MyWife));
                if (saveFlag)
                {
                    this.MyWife = WifeFactory.CreateWife(type);
                }
                return saveFlag;
            }
        }

        public bool ResetLastTimeWife()
        {
            var mementoWifie = this.wifeStack.Dequeue();
            if (mementoWifie==null)
            {
                return false;
            }
            mementoWifie.ResetWfieInfo(this.MyWife);
            return true;
        }
    }


    #region 简单工厂
    public class WifeFactory
    {
        public static WifeBase CreateWife(WifeType type)
        {
            WifeBase wife = null;
            switch (type)
            {
                case WifeType.X:
                    wife = new XishiWife();
                    break;
                case WifeType.W:
                    wife = new WZJWife();
                    break;
                case WifeType.D:
                    wife = new DiaoChanWife();
                    break;
                case WifeType.Y:
                    wife = new YYHWife();
                    break;
                default:
                    break;
            }

            return wife;
        }
    }

    public enum WifeType
    {
        X, W, D, Y,
    }

    #endregion
}
